package Parser;

import java.util.Vector;

/**
 * Title: Rules
 * Description: Parses Rules from set knowlegdbase file
 * @version 1.0
 */

public class Rules
{

  private String name;
  private String ifname, ifvalue, thenname, thenvalue;
  public Vector vecANDOR = new Vector();
  public Vector vecTHENMore = new Vector();

  public Rules(String name)
  {
    this.name = name;
  }

  public void setIf(String name, String value)
  {
    this.ifname = name;
    this.ifvalue = value;
  }

  public void setThen(String name, String value)
  {
    this.thenname = name;
    this.thenvalue = value;
  }

  public String getIf()
  {
    return "IF " + this.ifname + " IS " + this.ifvalue;
  }

  public String getThen()
  {
    return "\nTHEN " + this.thenname + " IS " + this.thenvalue;
  }

  public String GetName()
  {
    return this.name;
  }

  /*
   *  Funtionname:   checkANDOR
   *  desc:  filters out any AND/OR statements in an if
   */
  public void checkANDOR(String ifstr)
  {
    int wordcount = 0, StrSize = 1;
    String ANDname = "", ANDvalue = "", ORname = "", ORvalue = "";
    wordcount = this.getWordCount(ifstr);

    //objects
    ANDExpression tempAND;
    ORExpression tempOR;

    //while number is smaller than amount of words
    while (StrSize <= wordcount)
    {
      try
      {
        if (getWord(ifstr, StrSize).matches("AND"))
        {
          //AND found
          //System.out.println("AND found");

          if (getWord(ifstr, StrSize + 2).matches("IS"))
          {
            ANDname = getWord(ifstr, StrSize + 1);
            //System.out.println("AND Name\t" + getWord(ifstr, StrSize + 1));
            ANDvalue = getWord(ifstr, StrSize + 3);
            //System.out.println("AND Value\t" + getWord(ifstr, StrSize + 3));
            tempAND = new ANDExpression(ANDname, ANDvalue); //initiate object
            this.vecANDOR.add(tempAND);// adding AND object to AND vector
          }
          else
          { //No IS defined
            break;
          }
        }
      }
      catch (Exception e)
      {
      }

      try
      {
        if (getWord(ifstr, StrSize).matches("OR"))
        {
          //OR found
          System.out.println("OR found");
          if (getWord(ifstr, StrSize + 2).matches("IS"))
          {
            ORname = getWord(ifstr, StrSize + 1);
            //System.out.println("OR Name\t\t" + ORname);
            ORvalue = getWord(ifstr, StrSize + 3);
            //System.out.println("OR Name\t\t" + ORvalue);
            tempOR = new ORExpression(ORname, ORvalue); //initiate object
            this.vecANDOR.add(tempOR); // adding OR object to OR vector
          }
          else
          { //No IS defined
            break;
          }
        }
      }
      catch (Exception e)
      {
      }
      StrSize += 2; //end of loop
    }
  }

  public void checkThenMore(String thenstr)
  {
    int wordcount = 0, StrSize = 1;
    String ANDname = "", ANDvalue = "", ORname = "", ORvalue = "";
    wordcount = this.getWordCount(thenstr);

    //objects
    ANDExpression tempAND;

    //while number is smaller than amount of words
    while (StrSize <= wordcount)
    {
      try
      {
        if (getWord(thenstr, StrSize).matches("AND"))
        {
          //AND found
          //System.out.println("AND found");

          if (getWord(thenstr, StrSize + 2).matches("IS"))
          {
            ANDname = getWord(thenstr, StrSize + 1);
            //System.out.println("AND Name\t" + getWord(thenstr, StrSize + 1));
            ANDvalue = getWord(thenstr, StrSize + 3);
            //System.out.println("AND Value\t" + getWord(thenstr, StrSize + 3));
            tempAND = new ANDExpression(ANDname, ANDvalue); //initiate object
            this.vecTHENMore.add(tempAND);// adding AND object to AND vector
          }
          else
          { //No IS defined
            break;
          }
        }
      }
      catch (Exception e)
      {
      }

      StrSize += 2; //end of loop
    }
  }

  private String getWord(String setstr, int nr)
  {
    int currentWord = 0;
    int i = 0;

    //remove starting and ending spaces etc..
    setstr = setstr.trim();

    while(setstr.length() > 0)
    {
      if(i == setstr.length() || setstr.charAt(i) == '\n' || setstr.charAt(i) == ' ' || setstr.charAt(i) == '\t')
      {
        //einde van huidige woord gevonden
        if(nr == currentWord)
        {  //huidige woord moet worden teruggegeven
          return (setstr.substring(0, i)).trim();
        }
        //nog niet het goede woord, doorgaan met volgende woord zoeken
        setstr = (setstr.substring(i, setstr.length())).trim();
        i=0;
        currentWord++;
      }
      i++;
    }
    return null;
  }

  /*
   *  Funtionname:   getWordCount
   *  desc:  Count the words in an ifline
   */
  private int getWordCount(String setstr)
  {
    int currentWord = 0;
    int i = 0;

    //remove starting and ending spaces etc..
    setstr = setstr.trim();

    while (setstr.length() > 0)
    {
      if (i == setstr.length() || setstr.charAt(i) == '\n' || setstr.charAt(i) == ' ' || setstr.charAt(i) == '\t')
      {
        //einde van huidige woord gevonden
        setstr = (setstr.substring(i, setstr.length())).trim();
        i = 0;
        currentWord++;
      }
      i++;
    }
   // System.out.println(currentWord);
    return currentWord;
  }

  public Vector getVecAndOr()
  {
  	return vecANDOR;
  }

  public Vector getVecThenMore()
  {
  	return vecTHENMore;
  }

  public String toString()
  {
  	String strRes = "";
  	strRes = getIf();

  	if(!getVecAndOr().isEmpty())
  	{
  		for(int a = 0; a < getVecAndOr().size(); a++)
  		{
  			strRes = strRes + getVecAndOr().elementAt(a).toString();
  		}
  	}

  	strRes = strRes + getThen();

  	if(!getVecThenMore().isEmpty())
  	{
  		for(int a = 0; a < getVecThenMore().size(); a++)
  		{
  			strRes = strRes + getVecThenMore().elementAt(a).toString();
  		}
  	}

  	strRes = strRes + "\n";
  	return strRes;
  }
}
